package org.pay.manage.settlement.ctrl;

import com.alibaba.fastjson.JSONObject;
import org.pay.manage.common.ctrl.BaseController;
import org.pay.manage.common.service.RpcCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.pay.core.common.Exception.ServiceException;
import org.pay.core.common.constant.Constant;
import org.pay.core.common.constant.MchConstant;
import org.pay.core.common.constant.RetEnum;
import org.pay.core.common.domain.BizResponse;
import org.pay.core.common.domain.PayPageRes;
import org.pay.core.common.domain.PayResponse;
import org.pay.core.entity.MchSettBatchRecord;
import org.pay.core.entity.MchSettRecord;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: yangshancheng
 * @date: 17/12/6
 * @description:
 */
@RestController
@RequestMapping(Constant.MGR_CONTROLLER_ROOT_PATH + "/sett")
@PreAuthorize("hasRole('"+ MchConstant.MGR_ROLE_NORMAL+"')")
public class SettController extends BaseController {

    @Autowired
    private RpcCommonService rpcCommonService;

    /**
     * 结算列表查询
     * @return
     */
    @RequestMapping("/list")
    @ResponseBody
    public ResponseEntity<?> list(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        MchSettRecord settRecord = getObject(param, MchSettRecord.class);
        int count = rpcCommonService.rpcMchSettHandleService.count(settRecord);
        if(count == 0) ResponseEntity.ok(PayPageRes.buildSuccess());
        List<MchSettRecord> mchSettRecordList = rpcCommonService.rpcMchSettHandleService.select((getPageIndex(param)-1) * getPageSize(param), getPageSize(param), settRecord);
        return ResponseEntity.ok(PayPageRes.buildSuccess(mchSettRecordList, count));
    }

    /**
     * 审核列表查询
     * @return
     */
    @RequestMapping("/audit_list")
    @ResponseBody
    public ResponseEntity<?> auditList(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        MchSettRecord settRecord = getObject(param, MchSettRecord.class);
        List<Byte> settStatusList = new LinkedList<>();
        settStatusList.add(MchConstant.SETT_STATUS_AUDIT_ING); // 审核中
        settStatusList.add(MchConstant.SETT_STATUS_AUDIT_NOT); // 审核不通过
        int count = rpcCommonService.rpcMchSettHandleService.count(settStatusList, settRecord);
        if(count == 0) ResponseEntity.ok(PayPageRes.buildSuccess());
        List<MchSettRecord> mchSettRecordList = rpcCommonService.rpcMchSettHandleService.select((getPageIndex(param)-1) * getPageSize(param), getPageSize(param), settStatusList, settRecord);
        return ResponseEntity.ok(PayPageRes.buildSuccess(mchSettRecordList, count));
    }

    /**
     * 审核列表查询
     * @return
     */
    @RequestMapping("/remit_list")
    @ResponseBody
    public ResponseEntity<?> remitList(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        MchSettRecord settRecord = getObject(param, MchSettRecord.class);
        List<Byte> settStatusList = new LinkedList<>();
        settStatusList.add(MchConstant.SETT_STATUS_AUDIT_OK);   // 已审核
        settStatusList.add(MchConstant.SETT_STATUS_REMIT_ING);  // 打款中
        settStatusList.add(MchConstant.SETT_STATUS_REMIT_FAIL); // 打款失败
        int count = rpcCommonService.rpcMchSettHandleService.count(settStatusList, settRecord);
        if(count == 0) ResponseEntity.ok(PayPageRes.buildSuccess());
        List<MchSettRecord> mchSettRecordList = rpcCommonService.rpcMchSettHandleService.select((getPageIndex(param)-1) * getPageSize(param), getPageSize(param), settStatusList, settRecord);
        return ResponseEntity.ok(PayPageRes.buildSuccess(mchSettRecordList, count));
    }

    /**
     * 结算查询
     * @return
     */
    @RequestMapping("/get")
    @ResponseBody
    public ResponseEntity<?> get(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long id = getLongRequired(param, "id");
        MchSettRecord settRecord = rpcCommonService.rpcMchSettHandleService.findById(id);
        return ResponseEntity.ok(PayResponse.buildSuccess(settRecord));
    }

    /**
     * 审核
     * @return
     */
    @RequestMapping("/audit")
    @ResponseBody
    public ResponseEntity<?> audit(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long id = getLongRequired(param, "id");
        Byte status = getByteRequired(param, "status");
        String remark = getString(param, "remark");
        // 状态必须为审核通过或审核不通过
        if(status.byteValue() != MchConstant.SETT_STATUS_AUDIT_OK &&
                status.byteValue() != MchConstant.SETT_STATUS_AUDIT_NOT) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MGR_STATUS_ERROR));
        }
        try {
            int count = rpcCommonService.rpcMchSettHandleService.auditSett(id, status, remark);
            if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        }catch (ServiceException e) {
            return ResponseEntity.ok(BizResponse.build(e.getRetEnum()));
        }catch (Exception e) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_UNKNOWN_ERROR));
        }
    }

    /**
     * 打款
     * @return
     */
    @RequestMapping("/remit")
    @ResponseBody
    public ResponseEntity<?> remit(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long id = getLongRequired(param, "id");
        Byte status = getByteRequired(param, "status");
        String remark = getString(param, "remark");
        String remitRemark = getString(param, "remitRemark");
        // 状态必须为打款中|打款成功|打款失败
        if(status.byteValue() != MchConstant.SETT_STATUS_REMIT_ING
                && status.byteValue() != MchConstant.SETT_STATUS_REMIT_SUCCESS
                && status.byteValue() != MchConstant.SETT_STATUS_REMIT_FAIL) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_MGR_STATUS_ERROR));
        }
        try {
            int count = rpcCommonService.rpcMchSettHandleService.remit(id, status, remark, remitRemark);
            if(count == 1) return ResponseEntity.ok(BizResponse.buildSuccess());
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_OPERATION_FAIL));
        }catch (ServiceException e) {
            return ResponseEntity.ok(BizResponse.build(e.getRetEnum()));
        }catch (Exception e) {
            return ResponseEntity.ok(BizResponse.build(RetEnum.RET_COMM_UNKNOWN_ERROR));
        }
    }

    /**
     * 批量账户记录查询
     * @return
     */
    @RequestMapping("/batch_record_list")
    @ResponseBody
    public ResponseEntity<?> batchRecordList(HttpServletRequest request) {
        JSONObject param = getJsonParam(request);
        Long settRecordId = getLongRequired(param, "id");   // 结算记录ID
        MchSettBatchRecord mchSettBatchRecord = new MchSettBatchRecord();
        mchSettBatchRecord.setSettRecordId(settRecordId);
        int count = rpcCommonService.rpcMchSettBatchRecordService.count(mchSettBatchRecord);
        if(count == 0) ResponseEntity.ok(PayPageRes.buildSuccess());
        List<MchSettBatchRecord> mchSettBatchRecordList = rpcCommonService.rpcMchSettBatchRecordService.select((getPageIndex(param)-1) * getPageSize(param), getPageSize(param), mchSettBatchRecord);
        return ResponseEntity.ok(PayPageRes.buildSuccess(mchSettBatchRecordList, count));
    }

}
